using System;
using System.Linq;
using System.Text;
using DevExpress.Xpo;
using DevExpress.ExpressApp;
using System.ComponentModel;
using DevExpress.ExpressApp.DC;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.Base;
using System.Collections.Generic;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;

namespace DomainComponents.Common
{
    [DomainComponent]
    [NonPersistentDc]
    public interface IPhones
    {
        string OtherPhone { get; set; }
        string MobilePhone { get; set; }
        string OfficePhone { get; set; }
        string HomePhone { get; set; }
        string Fax { get; set; }

        //[DevExpress.ExpressApp.DC.Aggregated]
        //[VisibleInDetailView(false)]
        //IList<IPhone> Phones { get; }

        void CopyTo(IPhones targetPhones);
    }

    [DomainLogic(typeof(IPhones))]
    public class PhonesLogic
    {
        //public static Type PhoneObjectType = typeof(IPhone);

        //public const string OtherPhoneId = "Other";
        //public const string MobilePhoneId = "Mobile";
        //public const string OfficePhoneId = "Office";
        //public const string HomePhoneId = "Home";
        //public const string FaxId = "Fax";

        //public static void AfterConstruction(IPhones phones, IObjectSpace os) {
        //    IPhone phone1 = (IPhone)os.CreateObject(PhoneObjectType);
        //    phone1.PhoneType = OtherPhoneId;
        //    phones.Phones.Add(phone1);
        //    IPhone phone2 = (IPhone)os.CreateObject(PhoneObjectType);
        //    phone2.PhoneType = MobilePhoneId;
        //    phones.Phones.Add(phone2);
        //    IPhone phone3 = (IPhone)os.CreateObject(PhoneObjectType);
        //    phone3.PhoneType = OfficePhoneId;
        //    phones.Phones.Add(phone3);
        //    IPhone phone4 = (IPhone)os.CreateObject(PhoneObjectType);
        //    phone4.PhoneType = HomePhoneId;
        //    phones.Phones.Add(phone4);
        //    IPhone phone5 = (IPhone)os.CreateObject(PhoneObjectType);
        //    phone5.PhoneType = FaxId;
        //    phones.Phones.Add(phone5);
        //}

        public static void CopyTo(IPhones self, IObjectSpace objectSpace, IPhones targetPhones)
        {
            if (targetPhones == null)
            {
                return;
            }
            targetPhones.OtherPhone = self.OtherPhone;
            targetPhones.MobilePhone = self.MobilePhone;
            targetPhones.OfficePhone = self.OfficePhone;
            targetPhones.HomePhone = self.HomePhone;
            targetPhones.Fax = self.Fax;

            //foreach(IPhone phone in self.Phones) {
            //    if(targetPhones.Phones[phone.PhoneType] != null) {
            //        targetPhones.Phones[phone.PhoneType].Number = phone.Number;
            //    }
            //    else {
            //        IPhone targetPhone = (IPhone)objectSpace.CreateObject(PhonesLogic.PhoneObjectType);
            //        targetPhone.PhoneType = phone.PhoneType;
            //        targetPhones.Phones.Add(targetPhone);
            //    }
            //}
        }
        //public static string GetPhoneByType(IList<IPhone> phones, string phoneType) {
        //    foreach(IPhone phone in phones) {
        //        if(phone.PhoneType == phoneType)
        //            return phone.Number;
        //    }
        //    return null;
        //}

        //public static string Get_OtherPhone(IPhones phones) {
        //    return GetPhoneByType(phones.Phones, OtherPhoneId);
        //}
        //public static string Get_MobilePhone(IPhones phones) {
        //    return GetPhoneByType(phones.Phones, MobilePhoneId);
        //}
        //public static string Get_OfficePhone(IPhones phones) {
        //    return GetPhoneByType(phones.Phones, OfficePhoneId);
        //}
        //public static string Get_HomePhone(IPhones phones) {
        //    return GetPhoneByType(phones.Phones, HomePhoneId);
        //}
        //public static string Get_Fax(IPhones phones) {
        //    return GetPhoneByType(phones.Phones, FaxId);
        //}
    }

    //[DefaultClassOptions]
    ////[ImageName("BO_Contact")]
    ////[DefaultProperty("DisplayMemberNameForLookupEditorsOfThisType")]
    ////[DefaultListViewOptions(MasterDetailMode.ListViewOnly, false, NewItemRowPosition.None)]
    ////[Persistent("DatabaseTableName")]
    //// Specify more UI options using a declarative approach (http://documentation.devexpress.com/#Xaf/CustomDocument2701).
    //public class IPhone : BaseObject
    //{ // Inherit from a different class to provide a custom primary key, concurrency and deletion behavior, etc. (http://documentation.devexpress.com/#Xaf/CustomDocument3146).
    //    public IPhone(Session session)
    //        : base(session)
    //    {
    //    }
    //    public override void AfterConstruction()
    //    {
    //        base.AfterConstruction();
    //        // Place your initialization code here (http://documentation.devexpress.com/#Xaf/CustomDocument2834).
    //    }
    //    //private string _PersistentProperty;
    //    //[XafDisplayName("My display name"), ToolTip("My hint message")]
    //    //[ModelDefault("EditMask", "(000)-00"), Index(0), VisibleInListView(false)]
    //    //[Persistent("DatabaseColumnName"), RuleRequiredField(DefaultContexts.Save)]
    //    //public string PersistentProperty {
    //    //    get { return _PersistentProperty; }
    //    //    set { SetPropertyValue("PersistentProperty", ref _PersistentProperty, value); }
    //    //}

    //    //[Action(Caption = "My UI Action", ConfirmationMessage = "Are you sure?", ImageName = "Attention", AutoCommit = true)]
    //    //public void ActionMethod() {
    //    //    // Trigger a custom business logic for the current record in the UI (http://documentation.devexpress.com/#Xaf/CustomDocument2619).
    //    //    this.PersistentProperty = "Paid";
    //    //}
    //}
}
